<?php

class PetCategory
{    
	private static $IDs=array({IDs});

	public static function loadPetCategory($id)
	{
		if(!self::exists($id))
			throw new CException("unknown petCatetory[$id]");

		$classname='PetCategory_'.$id;
		return call_user_func(array($classname, 'getInstance'));
	}

	public static function exists($id)
	{
		return array_key_exists($id, self::$IDs);
	}

	public function createPet($options=array())
	{
		$pet=new Pet;
		$pet->name=$this->name;
		$pet->categoryId=$this->id;
		$pet->race=$this->race;
        
        if(isset($options['attackSkills']))
            $pet->attackSkills=$options['attackSkills']+$this->attackSkills;
        else
            $pet->attackSkills=$this->attackSkills;

        if(isset($options['helpSkills']))
            $pet->helpSkills=$options['helpSkills']+$this->helpSkills;
        else
            $pet->helpSkills = $this->helpSkills;

        foreach($pet->attackSkills as $k=>$l){
            if($l==0) unset($pet->attackSkills[$k]);
        }        
        foreach($pet->helpSkills as $k=>$l){
            if($l==0) unset($pet->helpSkills[$k]);
        }

        $pet->userName=$options['userName'];
        $pet->parentId=$options['parentId'];
        $pet->level=isset($options['level'])?$options['level']:1;
        $pet->xp=isset($options['xp'])?$options['xp']:0;
                    
		$this->createFeature($pet);
		$this->createAttribute($pet, isset($options['ratio'])? $options['ratio'] : 100);

		$pet->endurance=$pet->initEndurance;
		$pet->strength=$pet->initStrength;
		$pet->dexterity=$pet->initDexterity;
		$pet->spirit=$pet->initSpirit;

		$pet->calculateStatistic();

		if(isset($options['hp']))
            $pet->setHp($options['hp']);
        else 
            $pet->recoverHp();
			
		if(isset($options['mp']))
            $pet->setMp($options['mp']);
        else
            $pet->recoverMp();

		return $pet;
	}

    protected function createFeature(&$pet)
    {
        $keys=array_keys(Pet::$features);
        $n=count($keys);
        if(!$n)
            throw new Exception('no feature defined');
        $pet->feature=$keys[mt_rand(0,$n-1)];
    }

    protected function createAttribute(&$pet, $ratio)
    {
        //TODO: random init 4-base attributes by $petBase->award ?
        //TODO: create depending on $ratio
        $pet->initEndurance=$this->endurance;
        $pet->initStrength=$this->strength;
        $pet->initDexterity=$this->dexterity;
        $pet->initSpirit=$this->spirit;
    }
}

?>
